package org.xdq.阻塞队列;

import java.util.concurrent.ArrayBlockingQueue;

public class Test {

    static class Bread{
        private String num;
        private Bread(String num){
            this.num = num;
        }

        @Override
        public String toString() {
            return "Bread{" + num +'}';
        }
    }


    public static void main(String[] args) {
        ArrayBlockingQueue<Bread> queue = new ArrayBlockingQueue<>(5);

        for (int k = 1;k<=10;k++) {

            new Thread(()->{
                for(int i=0;i<100;i++){
                    try {
                        Bread b = new Bread(Thread.currentThread().getName()+"-"+(i+1));
                        System.out.println(Thread.currentThread().getName()+"：当前面包数为"+queue.size()+"，准备放入面包"+b);
                        queue.put(b);//放入一个元素
                        System.out.println(Thread.currentThread().getName()+"：已放入面包"+b+"，当前面包数为"+queue.size());
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            },"面包师"+k).start();

            new Thread(()->{
                for(int i=0;i<100;i++){
                    try {
                        System.out.println(Thread.currentThread().getName()+"：当前面包数为"+queue.size()+"，准备取出一个面包");
                        Bread b = queue.take();//取出一个元素
                        System.out.println(Thread.currentThread().getName()+"：已取出面包"+b+"，当前面包数为"+queue.size());
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            },"销售员"+k).start();
        }
    }


}
